import { Component } from '@angular/core';
import {
  IonicPage,
  NavController,
  NavParams,
  ActionSheetController,
  App
} from 'ionic-angular';
import { MySeriveProvider } from '../../../providers/my-serive/my-serive';
import { Events, LoadingController } from 'ionic-angular';
import { BaseUI } from '../../../common/baseUI';

@IonicPage()
@Component({
  selector: 'page-wd',
  templateUrl: 'wd.html'
})
export class WdPage extends BaseUI {
  avatar;
  nickname = '';
  realname;
  classes;
  userqq;
  phone;
  isShowInfo: boolean = false;
  user = {};
  backImgs;
  randomBackImg;
  backImg;

  constructor(
    public navCtrl: NavController,
    public navParams: NavParams,
    public actionSheetCtrl: ActionSheetController,
    public mySerive: MySeriveProvider,
    public loadingCtrl: LoadingController,
    public app: App,
    public events: Events
  ) {
    super();
    this.backImgs = [
      'http://netx-check.oss-cn-shenzhen.aliyuncs.com/photo/1533191152234.png',
      'http://netx-check.oss-cn-shenzhen.aliyuncs.com/photo/1533191257144.png',
      'http://netx-check.oss-cn-shenzhen.aliyuncs.com/photo/1533191273642.png',
      'http://netx-check.oss-cn-shenzhen.aliyuncs.com/photo/1533191291941.png',
      'http://netx-check.oss-cn-shenzhen.aliyuncs.com/photo/1533191306663.png',
      'http://netx-check.oss-cn-shenzhen.aliyuncs.com/photo/1533191333909.png',
      'http://netx-check.oss-cn-shenzhen.aliyuncs.com/photo/1533191352787.png',
      'http://netx-check.oss-cn-shenzhen.aliyuncs.com/photo/1533191367341.png',
      'http://netx-check.oss-cn-shenzhen.aliyuncs.com/photo/1533191376090.png',
      'http://netx-check.oss-cn-shenzhen.aliyuncs.com/photo/1533191388739.png',
      'http://netx-check.oss-cn-shenzhen.aliyuncs.com/photo/1533191406030.png',
      'http://netx-check.oss-cn-shenzhen.aliyuncs.com/photo/1533191415413.png',
      'http://netx-check.oss-cn-shenzhen.aliyuncs.com/photo/1533191426298.png',
      'http://netx-check.oss-cn-shenzhen.aliyuncs.com/photo/1533191445077.png',
      'http://netx-check.oss-cn-shenzhen.aliyuncs.com/photo/1533191457567.png',
      'http://netx-check.oss-cn-shenzhen.aliyuncs.com/photo/1533191468875.png'
    ];
    this.randomBackImg = this.backImgs[
      Math.floor(Math.random() * this.backImgs.length)
    ];
  }

  ionViewDidLoad() {
    this.getInfo();
    this.getBackImg();
  }

  // 获取个人信息
  getInfo() {
    // const loading = this.showLoading(this.loadingCtrl, '加载中');
    this.mySerive.MeInfo().subscribe(data => {
      // 把服务器对象传值给对象 初始化类型是必须是对象
      this.user = data;
      // loading.dismiss();
    });
  }

  ionViewWillEnter() {
    setTimeout(() => {
      this.getInfo();
    }, 100);
  }

  getBackImg() {
    if ((this.backImg = localStorage.getItem('backImg'))) {
      this.backImg = this.backImg = localStorage.getItem('backImg');
    } else {
      localStorage.setItem('backImg', this.randomBackImg);
      this.backImg = this.randomBackImg;
    }
  }

  isOut() {
    this.mySerive.checkAdmin().subscribe(data => {
      if (data['status'] === 200) {
        this.AdminActionSheet();    
      }else if (data['status'] === 400) {
        this.UserActionSheet();
      } 
    });
    
  }

  UserActionSheet() {
    let actionSheet = this.actionSheetCtrl.create({
      buttons: [
        {
          text: '修改密码',
          handler: () => {
            this.navCtrl.push('ConfirmPasswordPage', { phone: this.phone });
          }
        },
        {
          text: '退出登录',
          handler: () => {
            localStorage.removeItem('token');
            this.mySerive.logOff().subscribe(
              data => {
                if (data['status'] === 200) {
                  // localStorage.removeItem('token');
                  // this.mySerive.getToken('0');
                  this.app.getRootNav().setRoot('LoginOrRegisterPage');
                }
              },
              err => {
                this.mySerive.deleteToken();
                this.showLoading(this.loadingCtrl, '正在退出');
                this.app.getRootNav().setRoot('LoginOrRegisterPage');
              }
            );
          }
        }
      ]
    });
    actionSheet.present();
  }

  AdminActionSheet() {
    let actionSheet = this.actionSheetCtrl.create({
      buttons: [
        {
          text: '评测详情',
          handler: () => {
            this.navCtrl.push('DetailsPage');
          }
        },
        {
          text: '修改密码',
          handler: () => {
            this.navCtrl.push('ConfirmPasswordPage', { phone: this.phone });
          }
        },
        {
          text: '退出登录',
          handler: () => {
            localStorage.removeItem('token');
            this.mySerive.logOff().subscribe(
              data => {
                if (data['status'] === 200) {
                  // localStorage.removeItem('token');
                  // this.mySerive.getToken('0');
                  this.app.getRootNav().setRoot('LoginOrRegisterPage');
                }
              },
              err => {
                this.mySerive.deleteToken();
                this.showLoading(this.loadingCtrl, '正在退出');
                this.app.getRootNav().setRoot('LoginOrRegisterPage');
              }
            );
          }
        }
      ]
    });
    actionSheet.present();
  }

  gotoChangeAvatar() {
    this.navCtrl.push('ChangeAvatarPage', { avatar: this.user['avatar'] });
  }
}
